package middleware

import (
	"fmt"
	"github.com/zeromicro/go-zero/rest/httpx"
	"jdshopuser/api/internal/types"
	"net/http"
)

type AuthorTokenMiddleware struct {
}

func NewAuthorTokenMiddleware() *AuthorTokenMiddleware {
	return &AuthorTokenMiddleware{}
}

func (m *AuthorTokenMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO 身份验证

		token := r.Header.Get("token")
		fmt.Println("触发中间件")
		if token == "" {
			res := &types.RegMsgResponse{
				Cre:   -1,
				Msg:   "token为空",
				Datra: nil,
			}
			httpx.OkJsonCtx(r.Context(), w, res)
			return
		}

		// Passthrough to next handler if need
		next(w, r)
	}
}
